package com.jzl.swing.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.jzl.swing.dao.BillDao;
import com.jzl.swing.entity.Bill;
import com.jzl.swing.rowmapper.impl.BillRowMapper;
import com.jzl.swing.util.JDBCTemplate;

public class BillDaoImpl implements BillDao{

	@Override
	public int add(Bill bill) throws SQLException {
		String sql = "insert into bills(bill_number,bill_product_name,bill_product_number,bill_price,bill_ispaid,bill_supplierName,bill_product_decrib,bill_date)"
				+ "values(?,?,?,?,?,?,?,?)";
		return JDBCTemplate.executeUpdate(sql,bill.getBill_number(), bill.getBill_product_name(), bill.getBill_product_number(),bill.getBill_price(),
				bill.getBill_ispaid(),bill.getBill_supplierName(), bill.getBill_product_decrib(),bill.getBill_date());
	}

	@Override
	public int delete(Bill bill) throws SQLException {
		String sql = "delete from bills where bill_number=?";
		
		return JDBCTemplate.executeUpdate(sql, bill.getBill_number());
	}

	@Override
	public int modify(Bill bill) throws SQLException {
		String sql = "update bills set bill_product_name=?,bill_product_number=?,bill_price=?, bill_ispaid=?, bill_supplierName=?, bill_product_decrib=?, "
				+ "bill_date=? where bill_number=?";
		return JDBCTemplate.executeUpdate(sql, bill.getBill_product_name(), bill.getBill_product_number(),bill.getBill_price(),
				bill.getBill_ispaid(),bill.getBill_supplierName(), bill.getBill_product_decrib(),bill.getBill_date(),bill.getBill_number());
	}

	@Override
	public List<Bill> queryAll() throws SQLException {
		List<Bill> billList = new ArrayList<Bill>();
		String sql = "select * from bills";
		List<Object> list = JDBCTemplate.executeQuery(sql, new BillRowMapper());
		for (Object object : list) {
			billList.add((Bill) object);
		}
		return billList;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Bill> queryByNameAndPaid(String name, String paid) throws SQLException {
		String sql = "select * from bills where bill_product_name=? and bill_ispaid=?";
		List<?> list = JDBCTemplate.executeQuery(sql, new BillRowMapper(), name, paid);
		return (List<Bill>) list;
	}

	@Override
	public Bill queryByNumber(String number) throws SQLException {
		String sql = "select * from bills where bill_number=?";
		List<?> list = JDBCTemplate.executeQuery(sql, new BillRowMapper(), number);
		if (list.size()<1) {
			return null;
		}
		return (Bill) list.get(0);
	}

	@Override
	public int delete(String number) throws SQLException {
		String sql = "delete from bills where bill_number=?";
		JDBCTemplate.executeUpdate(sql, number);
		return 0;
	}

}
